<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html>
<html>
<head>
<title>组织机构管理</title>
<jsp:include page="/WEB-INF/inc/systemInc.jsp"></jsp:include>
<c:if test="${fn:contains(sessionInfo.moduleList, '/admin/dictionary/addDictionary')}">
  <script type="text/javascript">
      $.canAdd = true;
    </script>
</c:if>
<c:if test="${fn:contains(sessionInfo.moduleList, '/admin/dictionary/editDictionaryPage')}">
  <script type="text/javascript">
      $.canEdit = true;
    </script>
</c:if>
<c:if test="${fn:contains(sessionInfo.moduleList, '/admin/dictionary/delete')}">
  <script type="text/javascript">
      $.canDelete = true;
    </script>
</c:if>
<script type="text/javascript">
  var treeGrid;
  // 初始化datagrid
  $(function() {
    // 初始化数据
    treeGrid = $('#treeGrid')
        .treegrid(
            {
              url : '${pageContext.request.contextPath}/admin/organization/loadTreeGrid',
              idField : 'id',
              treeField : 'orgName',
              parentField : 'orgPid',
              initialState : "expandAll",
              fit : true,
              fitColumns : true,
              border : false,
              striped : true,
              rownumbers : true,
              pagination : false,
              singleSelect : true,
              columns : [ [
                  {
                    title : 'id',
                    field : 'id',
                    width : 50,
                    checkbox : true,
                    hidden : true
                  },
                  {
                    field : 'orgName',
                    title : '机构名称',
                    // 							state:'closed',
                    width : 150
                  },
                  {
                    field : 'orgAddress',
                    title : '地址',
                    width : 150
                  },
                  {
                    field : 'orgLeader',
                    title : '负责人',
                    width : 150
                  },
                  {
                    field : 'orgPhone',
                    title : '联系电话',
                    width : 250
                  },
                  {
                    field : 'orgDesc',
                    title : '备注',
                    width : 250
                  },
                  {
                    field : 'orgStatus',
                    title : '是否使用',
                    width : 250,
                    formatter : function(value, row) {
                      if (value && value == 1) {
                        return '使用';
                      }
                      return '废弃';
                    }
                  },
                  {
                    field : 'action',
                    title : '操作',
                    width : 100,
                    formatter : function(value, row, index) {
                      var str = '';
                      // 								if ($.canEdit) {
                      str += $
                          .formatString('<img onclick="editFun(\'{0}\');" src="{1}" title="修改"/>',
                              row.id,
                              '${pageContext.request.contextPath}/resources/css/images/extjs_icons/pencil.png');
                      // 								}
                      str += '&nbsp;';
                      // 								if ($.canDelete) {
                      str += $
                          .formatString(
                              '<img onclick="deleteFun(\'{0}\');" src="{1}" title="删除"/>', row.id,
                              '${pageContext.request.contextPath}/resources/css/images/extjs_icons/delete.png');
                      // 								}
                      return str;
                    }
                  } ] ],
              toolbar : '#toolbar',
              onContextMenu : function(e, row) {
                e.preventDefault();
                $(this).treegrid('unselectAll');
                $(this).treegrid('select', row.id);
                $('#menu').menu('show', {
                  left : e.pageX,
                  top : e.pageY
                });
              },
              onLoadSuccess : function() {
                parent.$.messager.progress('close');
                $(this).treegrid('tooltip');
              },
              onLoadError : function() {
                $.messager.confirm('提示', '您未登录或者长时间没有操作，请重新登录！', function(result) {
                  if (result) {
                    parent.location.href = '${pageContext.request.contextPath}/adminlogin.jsp';
                  }
                });
              }
            });

  });

  // 添加组织机构
  function addFun() {
    parent.$.modalDialog({
      title : '添加组织机构项',
      width : 550,
      height : 280,
      href : '${pageContext.request.contextPath}/admin/organization/addPage',
      buttons : [ {
        text : '添加',
        handler : function() {
          parent.$.modalDialog.openner_treeGrid = treeGrid;//因为添加成功之后，需要刷新这个treeGrid，所以先预定义好
          var f = parent.$.modalDialog.handler.find('#form');
          f.submit();
        }
      } ]
    });
  }

  //判断是否有子集，若有子集将子集收起
  function TreeGridShow(data) {
    $(data).each(function(index, item) {
      if (item.pid == '0') {
        //           			 item.state = "open";
        item.state = "closed";
      } else {
        treeGrid.treegrid('collapseAll');
        item.state = "open";
        //           			 TreeGridShow(item.children);
      }
    });
    return data;
  }

  // 修改组织机构
  function editFun(id) {
    if (id != undefined) {
      treeGrid.treegrid('select', id);
    }
    var node = treeGrid.treegrid('getSelected');
    if (node) {
      parent.$.modalDialog({
        title : '修改组织机构',
        width : 550,
        height : 280,
        href : '${pageContext.request.contextPath}/admin/organization/editPage?id=' + node.id
            + '&orgPid=' + node.orgPid,
        buttons : [ {
          text : '修改',
          handler : function() {
            parent.$.modalDialog.openner_treeGrid = treeGrid;//因为添加成功之后，需要刷新这个treeGrid，所以先预定义好
            var f = parent.$.modalDialog.handler.find('#form');
            f.submit();
          }
        } ]
      });
    }
  }

  // 删除角色
  function deleteFun(id) {
    if (id != undefined) {
      treeGrid.treegrid('select', id);
    }
    var node = treeGrid.treegrid('getSelected');
    if (node) {
      parent.$.messager.confirm('确认', '您确定要删除所选机构吗？', function(b) {
        if (b) {
          parent.$.messager.progress({
            title : '提示',
            text : '数据处理中，请稍后....'
          });
          $.post('${pageContext.request.contextPath}/admin/organization/delete', {
            id : node.id
          }, function(result) {

            if (result.success) {
              parent.$.messager.alert('成功', result.msg, 'info');
              treeGrid.treegrid('reload');
            } else {
              parent.$.messager.alert('错误', result.msg, 'error');
            }

            parent.$.messager.progress('close');
          }, 'JSON');
        }
      });
    }
  }

  // 展开菜单
  function redo() {

    var node = treeGrid.treegrid('getSelected');
    if (node) {
      treeGrid.treegrid('expandAll', node.id);
    } else {
      treeGrid.treegrid('expandAll');
    }
  }

  // 折叠菜单
  function undo() {
    var node = treeGrid.treegrid('getSelected');
    if (node) {
      treeGrid.treegrid('collapseAll', node.id);
    } else {
      treeGrid.treegrid('collapseAll');
    }
  }
</script>
</head>
<body>
  <div id="toolbar" style="display: none;">
    <%--     <c:if test="${fn:contains(sessionInfo.moduleList, '/admin/dictionary/addDictionary')}"> --%>
    <a href="javascript:void(0);" onclick="addFun();" class="easyui-linkbutton"
      data-options="plain:true,iconCls:'pencil_add'">增加</a>
    <%--     </c:if> --%>
    <a onclick="redo();" href="javascript:void(0);" class="easyui-linkbutton"
      data-options="plain:true,iconCls:'resultset_next'">展开</a> <a onclick="undo();"
      href="javascript:void(0);" class="easyui-linkbutton"
      data-options="plain:true,iconCls:'resultset_previous'">折叠</a><a
      onclick="treeGrid.treegrid('reload');" href="javascript:void(0);" class="easyui-linkbutton"
      data-options="plain:true,iconCls:'database_refresh'">刷新</a>
  </div>

  <div class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'center',border:false">
      <table id="treeGrid"></table>
    </div>
  </div>
  <div id="menu" class="easyui-menu" style="width: 120px; display: none;">
    <c:if test="${fn:contains(sessionInfo.moduleList, '/dictionaryController/editDictionaryPage')}">
      <div onclick="editFun();" data-options="iconCls:'pencil'">修改</div>
    </c:if>
    <c:if test="${fn:contains(sessionInfo.moduleList, '/dictionaryController/deleteDictionary')}">
      <div onclick="deleteFun();" data-options="iconCls:'delete'">删除</div>
    </c:if>
  </div>

</body>
</html>